At the beginning, we will introduce the basic syntax of Java, including variables, loops, functions, etc. We will enter object-oriented teaching almost in the last two weeks, introducing some class objects and related advanced concepts. This is done in the hope that users with no programming experience at all can learn a programming language from the basic to the advanced and use it as a tool in the future.
(This series of tutorials references W3Schools.)
In Java, every program starts with a class name, and every executable instruction must be placed inside that class. Additionally, the name of the .java file must match the class name.
//Filename : Main.java
public class Main{
public static void main(String[] args){
System.out.println("Hello IThome");
}
}
public class Main{}
Here public means and can be accessed by other classes at the same time, as described in more detail below.
→ Here public means and can be accessed by other classes at the same time, as described in more detail below.
public static void main(String[ ] args){}
main can be regarded as the main program (which must exist for every program), and all instructions to be executed will be placed in it.
→Other keywords such as static and void will be introduced later, so for now, just remember the function of the program.
System.out.println(”Hello IThome”) ;
This is to inform the system to print the content in ( ), and output it to the screen using the println() command
→This line is the content to be executed, and it is also placed in main (remember to place the content to be output in “ ”)
→Because the end of a line, we must inform the computer that this line has ended, so we add ; at the end.
// This is a single-line comment
/* This is a multi-line comment*/
In any language, we will face projects with complex code that is divided into different files. Annotation is therefore an important step at this time, not only to help you quickly understand the content, but also to help others understand your code.
There are two ways to comment in Java:
We can also perform mathematical operations using println().
System.out.println(126 + 919); //Outputs : 1045
System.out.println(126 * 919); //Outputs : 115794
In addition, there is another keyword, print(). The big difference is that the output will not wrap.
→ less ln, meaning new line.
System.out.print("Hello ");
System.out.print("World"); //Outputs : Hello World
System.out.println("Hello ");
System.out.println("World"); /* Outputs : Hello
World */
If there are any errors in the above content, please do not hesitate to let me know, thank you!!!!